package com.haiwen.cookie.servletcontext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 韩要雷
 */
@WebServlet("/context2")
public class Context2 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //1.获取servletContext 对象
        ServletContext servletContext = req.getServletContext();

        //2.设置域对象
        servletContext.setAttribute("name","admin");

        //3.获取域对象
        String name = (String) servletContext.getAttribute("name");
        System.out.println(name);

        //4.移除域对象
        servletContext.removeAttribute("name");
        System.out.println(name);
    }
}
